package cn.hwua.dao.impl;

import cn.hwua.dao.CommentDao;
import cn.hwua.entity.Comment;
import cn.hwua.util.DBUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class CommentDaoImpl implements CommentDao {
    Connection conn=DBUtil.getConnection();
    QueryRunner qr=new QueryRunner();
    @Override
    public int save(Comment obj) {
        String sql="INSERT INTO  tb_comment(id,userName,time,sortID,content,food_id) VALUES (?,?,?,?,?,?)";
        Object[] paras={obj.getId(),obj.getUserName(),obj.getTime(),obj.getSortID(),obj.getContent(),obj.getFood_id()};
        try {
            int row = qr.update(conn, sql, paras);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int update(Comment obj) {
        String sql="UPDATE  tb_comment SET  userName=?,time=?,sortID=?,content=?,food_id=? WHERE id=? ";
        Object[] paras={obj.getUserName(),obj.getTime(),obj.getSortID(),obj.getContent(),obj.getFood_id(),obj.getId()};
        try {
            int row = qr.update(conn, sql, paras);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int delete(Comment obj) {
        String sql="DELETE FROM tb_comment WHERE id  = ? ";
        Object[] paras={obj.getId()};
        try {
            int row = qr.update(conn, sql, paras);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Comment findById(Object id) {
        String sql="SELECT * FROM tb_comment where id  = ?";
        Object[] paras={id};
        try {
            Comment comment=qr.query(conn,sql,paras,new BeanHandler<Comment>(Comment.class));
            return comment;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Comment> findAll() {
        String sql="SELECT * FROM tb_comment";
        Object[] paras={};
        try {
            List<Comment> commentList=qr.query(conn,sql,paras,new BeanListHandler<Comment>(Comment.class));
            return commentList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Comment> findSearch(String keyword) {
        String sql="SELECT * FROM tb_comment where userName like concat('%',concat(?,'%'))";
        Object[] paras={keyword};
        try {
            List<Comment> commentList=qr.query(conn,sql,paras,new BeanListHandler<Comment>(Comment.class));
            return commentList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Comment> findPage(int page, int pagesize) {
        String sql="SELECT * FROM tb_comment limit ?,?";
        Object[] paras={(page-1)*pagesize,pagesize};
        try {
            List<Comment> commentList=qr.query(conn,sql,paras,new BeanListHandler<Comment>(Comment.class));
            return commentList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public long count() {
        String sql="select count(*) from tb_comment";
        Object[] paras={};
        try {
            long nums=qr.query(conn,sql,paras,new ScalarHandler<Long>());
            return nums;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
